package com.ardf.controller;

import com.ardf.entity.dto.RuleInfoDto;
import com.ardf.entity.dto.RuleProjectMenuDto;
import com.ardf.entity.dto.RuleTableDto;
import com.ardf.entity.vo.RuleUpdateVo;
import com.ardf.service.IRuleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/rule")
public class RuleController {

    private final IRuleService ruleService;

    public RuleController(IRuleService ruleService) {
        this.ruleService = ruleService;
    }

    /**
     * 分页
     */
    @GetMapping("/searchAll")
    public ResponseEntity<Page<RuleTableDto>> searchAll(@RequestParam("pageSize") Integer pageSize,
                                                              @RequestParam("pageNum") Integer pageNum,
                                                              @RequestParam("competitionId") Long competitionId,
                                                              @RequestParam(value = "projectId", required = false) Long projectId) {
        Page<RuleTableDto> page;
        page = ruleService.searchAll(pageSize, pageNum, competitionId, projectId);
        return ResponseEntity.ok(page);
    }
    /**
     * 获取项目菜单
     */
    @GetMapping("/getProjectMenu")
    public ResponseEntity<List<RuleProjectMenuDto>> getProjectMenu(@RequestParam("competitionId") Long competitionId){
        return ResponseEntity.ok(ruleService.getProjectMenu(competitionId));
    }
    /**
     * 更新规则
     */
    @PostMapping("/update")
    public void updateRule(@RequestBody RuleUpdateVo ruleUpdateVo){
        ruleService.updateRule(ruleUpdateVo);
    }
    /**
     * 导出规则表
     */
    @GetMapping("/download")
    public void download(HttpServletResponse response, @RequestParam("competitionId") Long competitionId) throws IOException {
        ruleService.download(response,competitionId);
    }
    /**
     * 获取规则信息（比赛详情页面需要）
     */
    @GetMapping("/getRuleInfo")
    public ResponseEntity<RuleInfoDto> getRuleInfo(@RequestParam("ruleId") Long ruleId){
        RuleInfoDto ruleInfo=ruleService.getRuleInfo(ruleId);
        return ResponseEntity.ok(ruleInfo);
    }
}
